//
//  NRBreadcrumTableView.m
//  NetrainFrame
//
//  Created by Gin on 2021/3/9.
//

#import "NRBreadcrumView.h"
#import "NRDesignateGroupModel.h"

@interface NRBreadcrumView()<UITableViewDelegate, UITableViewDataSource>

@property(nonatomic, strong) UITableView *tableView;

@end

@implementation NRBreadcrumView

- (instancetype)initWithFrame:(CGRect)frame{
    if(self = [super initWithFrame:frame]){
        [self addSubview:self.tableView];
    }
    return self;
}

- (void)setBreadcrumArray:(NSArray *)breadcrumArray{
    _breadcrumArray = breadcrumArray;
    [self.tableView reloadData];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.breadcrumArray.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if(self.viewDelegate && [self.viewDelegate respondsToSelector:@selector(breadcrumViewDidSelectedGroupModel:)]){
        NRDesignateGroupModel *groupModel = [self.breadcrumArray objectAtIndex:indexPath.section];
        [self.viewDelegate breadcrumViewDidSelectedGroupModel:groupModel];
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    NRBreadcrumTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([NRBreadcrumTableViewCell class])];
    // cell顺时针旋转90度
    cell.contentView.transform = CGAffineTransformMakeRotation(M_PI_2);
    NRDesignateGroupModel *groupModel = [self.breadcrumArray objectAtIndex:indexPath.section];
    cell.nameLabel.text = groupModel.name;
    if(indexPath.section == 0){
        cell.nameLabel.textColor = [UIColor colorWithHexString:@"#2D6AE1"];
    }else{
        cell.nameLabel.textColor = [UIColor colorWithHexString:@"#666666"];
    }
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    NRDesignateGroupModel *groupModel = [self.breadcrumArray objectAtIndex:indexPath.section];
    CGFloat width = [groupModel.name widthForFont:UIFontMake(14)];
    return width + 10;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 14, self.width)];
    UIImageView *imageView = [[UIImageView alloc] init];
    imageView.transform = CGAffineTransformMakeRotation(M_PI_2);
    imageView.image = UIImageMake(@"ic_assign_more_02");
    [view addSubview:imageView];
    [imageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.equalTo(view);
        make.width.mas_equalTo(14);
        make.height.mas_equalTo(20);
    }];
    return view;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    if(section == self.breadcrumArray.count - 1) return 0;
    return 14;
}


- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    return nil;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 0;
}

#pragma mark ----- 横向tableView
-(UITableView *)tableView {
    if(!_tableView){
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.height, self.width) style:UITableViewStyleGrouped];
        _tableView.contentInset = UIEdgeInsetsMake(14, 0, 14, 0);
        _tableView.backgroundColor = UIColor.whiteColor;
        _tableView.center = CGPointMake(self.width / 2, self.height / 2);
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.showsVerticalScrollIndicator = NO;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.transform = CGAffineTransformMakeRotation(-M_PI_2);
        [_tableView registerClass:[NRBreadcrumTableViewCell class] forCellReuseIdentifier:NSStringFromClass([NRBreadcrumTableViewCell class])];
    }
    return _tableView;
}

@end

@implementation NRBreadcrumTableViewCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    if(self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]){
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        UILabel *nameLabel = [[UILabel alloc] init];
        nameLabel.textAlignment = NSTextAlignmentCenter;
        nameLabel.font = UIFontMake(14);
        nameLabel.textColor = [UIColor colorWithHexString:@"#666666"];
        [self.contentView addSubview:nameLabel];
        self.nameLabel = nameLabel;
        
        [nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.center.equalTo(self.contentView);
        }];
    }
    return self;
}

- (void)setName:(NSString *)name{
    self.nameLabel.text = name;
}
@end
